package cn.nb12.xbox.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.nb12.xbox.po.Area;
import cn.nb12.xbox.reqpo.AreaAdd;
import cn.nb12.xbox.reqpo.AreaEdit;
import cn.nb12.xbox.respo.GenResult;
import cn.nb12.xbox.service.AreaService;
import cn.nb12.xbox.utils.ExchangeRateUtil;

@Controller
public class AreaController extends BaseController{

	@Autowired
	private AreaService areaService;

	/**
	 * 进入添加地区页面
	 * @param view
	 * @return
	 */
	@RequestMapping("area_goAdd")
	public ModelAndView goAdd(ModelAndView view) {
		view.setViewName("pages/area/area_goAdd");
		return view;
	}

	/**
	 * 执行添加操作
	 * @param areaAdd
	 * @param bindingResult
	 * @return
	 */
	@ResponseBody
	@RequestMapping("area_doAdd")
	public GenResult doAdd(@Valid AreaAdd areaAdd, BindingResult bindingResult,HttpServletRequest request) {
		try {
			GenResult result=new GenResult();
			if (bindingResult.hasErrors()) {
				List<ObjectError> allErrors = bindingResult.getAllErrors();
				StringBuffer errormsg=new StringBuffer();
				if(allErrors!=null && allErrors.size()>0) {
					for (ObjectError objectError : allErrors) {
						errormsg.append(objectError.getDefaultMessage());
					}
				}
				result.setType("0");
				result.setMsg(errormsg.toString());
				return result;
			}

			Area area = new Area();
			BeanUtils.copyProperties(areaAdd, area);
			int count = areaService.insertArea(area);
			if(count!=1) {
				throw new Exception("error.code.0000000002");//添加失败
			}
			return new GenResult("1", exceptionInfo.getMessage(request, messageSource, "msg.code.0000000003"));
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			GenResult result=new GenResult();
			result.setType("0");
			result.setMsg(exceptionInfo.getMessage(request, messageSource, e.getMessage()));
			return result;
		}
	}
	
	/**
	 * 进入修改地区页面
	 * @return
	 */
	@RequestMapping("area_goAreaEdit")
	public ModelAndView goEdit(String id,ModelAndView view,HttpServletRequest request) {
		try {
			//根据id查交易
			Area area = areaService.selectAreaById(id);
			if(area==null) {
				throw new Exception("error.code.0000000001");//没有找到原业务
			}
			view.getModel().put("area", area);
			view.setViewName("pages/area/area_goEdit");
			return view;
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			view.setViewName("pages/comm/error");
			view.getModel().put("msg", exceptionInfo.getMessage(request, messageSource, e.getMessage()));
			return view; 
		}
	}
	
	/**
	 * 执行编辑操作
	 * @param areaAdd
	 * @param bindingResult
	 * @return
	 */
	@ResponseBody
	@RequestMapping("area_doEdit")
	public GenResult doEdit(@Valid AreaEdit areaEdit, BindingResult bindingResult,HttpServletRequest request) {
		try {
			GenResult result=new GenResult();
			if (bindingResult.hasErrors()) {
				List<ObjectError> allErrors = bindingResult.getAllErrors();
				StringBuffer errormsg=new StringBuffer();
				if(allErrors!=null && allErrors.size()>0) {
					for (ObjectError objectError : allErrors) {
						errormsg.append(objectError.getDefaultMessage());
					}
				}
				result.setType("0");
				result.setMsg(errormsg.toString());
				return result;
			}

			Area area = new Area();
			BeanUtils.copyProperties(areaEdit, area);
			int count = areaService.updateNonEmptyAreaById(area);
			if(count!=1) {
				throw new Exception("error.code.0000000004");//编辑失败
			}
			return new GenResult("1", exceptionInfo.getMessage(request, messageSource, "msg.code.0000000004"));
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			GenResult result=new GenResult();
			result.setType("0"); 
			result.setMsg(exceptionInfo.getMessage(request, messageSource, e.getMessage()));
			return result;
		}
	}
	
	/**
	 * 展示地区列表
	 * @param view
	 * @return
	 */
	@RequestMapping("area_goMain")
	public ModelAndView goMain(ModelAndView view) {
		//获得当前时间
		String date=new SimpleDateFormat("yyyyMMdd").format(new Date());
		List<Area> list = areaService.selectArea(null);
		if(list!=null && list.size()>0) {
			for (Area area : list) {
				if(!date.equals(area.getUpdatetime())) {
					//更新费率
					String exchangeRate = ExchangeRateUtil.getExchangeRate(area.getCurrency(), "CNY");
					area.setRate(Double.parseDouble(exchangeRate));
					area.setUpdatetime(date);
					areaService.updateNonEmptyAreaById(area);
				}
			}
		}
		view.setViewName("pages/area/area_goMain");
		view.getModel().put("data", list);
		return view;
	}
}
